package com.my.controller;

import com.my.mapper.RightColumnMapper;
import com.my.mapper.RightMapper;
import com.my.pojo.Right;
import com.my.pojo.RightColumn;
import com.my.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.rmi.server.ExportException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/right")
public class RightController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        String result="";
        SqlSession sqlSession = MyBatisUtil.openSession();
        if (action.equals("preAdd")){
            RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
            List<RightColumn> rightColumnList = rightColumnMapper.searcherAllRightColumn();
            if(rightColumnList ==null){
                result ="error.jsp";
            }else {
                session.setAttribute("allRightColumn",rightColumnList);
                result ="right/addRight.jsp";
            }
        }
        if(action.equals("add")){
            String rightColumnIdStr = req.getParameter("rightColumnId");
            int rightColumnId = Integer.parseInt(rightColumnIdStr);
            String rightName = req.getParameter("rightName");
            String rightDesc = req.getParameter("rightDesc");
            String url = req.getParameter("url");
            Right right = Right.builder().rightColumnId(rightColumnId).rightName(rightName).rightDesc(rightDesc).url(url).build();
            RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
            Boolean isExists = rightMapper.isExistRight(rightName);
            if(isExists == null){
                rightMapper.addRight(right);
                sqlSession.commit();
                try {
                    List<Right> list = this.searchAllRightColumnAndRight();
                    if(list == null){
                        result ="error.jsp";
                    }else {
                        session.setAttribute("allRight",list);
                        result ="right/showAllRight.jsp";
                    }
                }catch (Exception e){
                    throw  new RuntimeException(e);
                }
            }else {
                result = "error.jsp";
            }
        }
        if(action.equals("showAllRight")){
            try{
                List<Right> list = new ArrayList<Right>();
                list = this.searchAllRightColumnAndRight();
                if(list == null){
                    result = "error.jsp";
                }else {
                    session.setAttribute("allRight",list);
                    result = "right/showAllRight.jsp";
                }
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
        resp.sendRedirect(result);
    }

    private List searchAllRightColumnAndRight() throws IOException{
        SqlSession sqlSession = MyBatisUtil.openSession();
        RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
        List<RightColumn> rightColumnList = rightColumnMapper.searcherAllRightColumn();
        RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
        List list3 = new ArrayList();
        for (int i =0; i<rightColumnList.size();i++){
            RightColumn rightColumn = rightColumnList.get(i);
            String rightColumnName = rightColumn.getRightColumnName();
            int rightColumnId = rightColumn.getId();
            List<Right> rightList = rightMapper.searchAllRightByRightColumnId(rightColumnId);
            List list1 = new ArrayList();
            List list2 = new ArrayList();
            list1.add(rightColumnName);
            list2.add(list1);
            list2.add(rightList);
            list3.add(list2);
        }
        return list3;
    }
}
